/*
 * Copyright 2014 Jonathan Haas
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package app.grinreader.library;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import app.grinreader.model.BookPosition;
import nl.siegmann.epublib.domain.Author;
import nl.siegmann.epublib.domain.Resource;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Kappich Systemberatung
 * @version $Revision: 0000 $
 */
public class BookData implements Serializable{
    private static final long serialVersionUID = -6567760510599054752L;
    private transient Bitmap thumbnail;
    private String title;
    private List<?> authors;
    private boolean dummy;
	private BookPosition readingPosition;

    public BookData() {
        title = null;
        authors = null;
    }

    public BookData(String title, List<Author> authors, Resource coverImage) {
        this.title = title;
        this.authors = new ArrayList<Author>(authors);
        if(coverImage != null){
            try {
                try {
//                    this.thumbnail = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeStream(inputStream), 256, 256);
                    BitmapFactory.Options opts = new BitmapFactory.Options();
                    opts.inJustDecodeBounds = true;
                    BitmapFactory.decodeStream(coverImage.getInputStream(), null, opts);
                    int outHeight = opts.outHeight;
                    opts.inJustDecodeBounds=false;
                    opts.inSampleSize = outHeight / 256;
                    this.thumbnail = BitmapFactory.decodeStream(coverImage.getInputStream(), null, opts);

                }
                finally {
                    coverImage.getInputStream().close();
                }
            } catch (IOException e) {

            }
        }
    }

    public boolean isDummy() {
        return dummy;
    }

    public void setDummy(boolean dummy) {
        this.dummy = dummy;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public List<Author> getAuthors() {
        // We may have deserialized weird objects
        final List<Author> result = new ArrayList<Author>();
        for (Object a : authors) {
            if (a instanceof Author) {
                result.add((Author) a);
            }
        }
        return result;
    }

    public void setAuthors(List<Author> authors) {
        this.authors = authors;
    }

    public Bitmap getThumbnail() {
        return thumbnail;
    }

    public void setThumbnail(Bitmap thumbnail) {
        this.thumbnail = thumbnail;
    }

	public BookPosition getReadingPosition() {
		return readingPosition;
	}

	public void setReadingPosition(BookPosition readingPosition) {
		this.readingPosition = readingPosition;
	}
}
